﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exportation;

namespace DAL
{
    public partial class ExpenseReport
    {
        public static IQueryable<ExpenseReport> LoadSearchResult(string name, string state)
        {
            DataAccessDataContext db = new DataAccessDataContext();
            
            return from e in db.ExpenseReports
                   where e.Name.Contains(name) && e.State.Contains(state)
                   select e;
        }

        public static void Delete(Guid id)
        {
            //apagar as linhas
            DataAccessDataContext db = new DataAccessDataContext();
            ExpenseReport report = db.ExpenseReports.FirstOrDefault(r => r.Id == id);
            if (report != null)
            {
                db.ExpenseReports.DeleteOnSubmit(report);
                foreach (ExpenseLine el in report.ExpenseLines)
                {
                    ExpenseLine.Delete(el.Id);
                }
            }
            db.SubmitChanges();
        }

        public static void Submit(Guid id)
        {
            DataAccessDataContext db = new DataAccessDataContext();
            ExpenseReport report = db.ExpenseReports.FirstOrDefault(r => r.Id == id);
            if (report != null)
            {
                //db.ExpenseReports.Attach(report);
                report.State = "Processing";
            }
            db.SubmitChanges();
        }

        public static IEnumerable<ExpenseReport> GetAll(String email)
        {
            IEnumerable<ExpenseReport> reports = null;
            DataAccessDataContext da = new DataAccessDataContext();

            var query = from e in da.ExpenseReports
                        where e.UserEmail.Equals(email)
                        select e;
            if (query != null)
                reports = query.AsEnumerable<ExpenseReport>();

            return reports;
        }

        public static IEnumerable<ExpenseReportState> GetAllStates()
        {
            DataAccessDataContext db = new DataAccessDataContext();
            return from e in db.ExpenseReportStates
                   select e;
        }

        public void Save()
        {
            using (DataAccessDataContext dc = new DataAccessDataContext())
            {
                var q = from e in dc.ExpenseReports
                        where e.Id.Equals(this.Id)
                        select e;
                int count = q.Count<ExpenseReport>();
                ExpenseReport er = null;
                if(count == 1)
                    er = q.First<ExpenseReport>();
                if (er == null)
                {
                    dc.ExpenseReports.InsertOnSubmit(this);
                }
                else
                {
                    //er = new ExpenseReport();
                    er.Name = this.Name;
                    er.Comments = this.Comments;
                    er.State = "Waiting";
                }
                dc.SubmitChanges();
            }
        }

        public static ExpenseReport Get(Guid id)
        {
            DataAccessDataContext da = new DataAccessDataContext();
            return (
                        from e in da.ExpenseReports
                        where e.Id.Equals(id)
                        select e
                    ).Single<ExpenseReport>();
        }

        public void Export(String exportationType)
        {
            DataAccessDataContext da = new DataAccessDataContext();
            String typeStr = (
                                from t in da.ExportationTypes
                                where t.Name.Equals(exportationType)
                                select t.TypeName
                              ).Single<String>();
            IExportation ee = ExportationFactory.GetExportationType(typeStr);
            String path = ee.Export(this);
            ExpenseExportation expExport = new ExpenseExportation();
            expExport.Path = path;
            expExport.ReportId = this.Id;
            expExport.Type = exportationType;
            da.ExpenseExportations.InsertOnSubmit(expExport);
            da.SubmitChanges();
        }
    }
}
